package com.cgnpc.scp.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.manager.mybatis.entity.DataDict;
import com.cgnpc.scp.manager.mybatis.entity.Systemconfig;
import com.cgnpc.scp.manager.mybatis.entity.Systeminfo;
import com.cgnpc.scp.manager.mybatis.mapper.client.SystemconfigMapper;
import com.cgnpc.scp.manager.mybatis.mapper.client.SysteminfoMapper;
import com.cgnpc.scp.manager.service.SystemInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SystemInfoServiceImpl implements SystemInfoService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SystemInfoServiceImpl.class);

    @Autowired
    private SysteminfoMapper systeminfoMapper;
    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private SystemconfigMapper systemconfigMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveOrUpdate(Systeminfo systeminfo) throws Exception {
        int updateRows = 0;
        UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
        String loginUserInfoUserId = loginUserInfo.getUserId();
        String loginUserInfoUserName = loginUserInfo.getUserName();

        if (StringUtil.isNullOrSpace(systeminfo.getId())) {
            systeminfo.setId(IDHelper.uuid());
            systeminfo.setCreateUser(loginUserInfoUserId);
            systeminfo.setCreateUserName(loginUserInfoUserName);
            systeminfo.setCreateTime(new Date());
            systeminfo.setLastUpdateUser(loginUserInfoUserId);
            systeminfo.setLastUpdateUserName(loginUserInfoUserName);
            systeminfo.setLastUpdateTime(new Date());
            updateRows = systeminfoMapper.insert(systeminfo);
        } else {
            systeminfo.setLastUpdateUser(loginUserInfoUserId);
            systeminfo.setLastUpdateUserName(loginUserInfoUserName);
            systeminfo.setLastUpdateTime(new Date());
            updateRows = systeminfoMapper.updateById(systeminfo);
        }
        return updateRows;
    }

    @Override
    public Systeminfo loadByID(Systeminfo systemInfo) throws Exception {
        return systeminfoMapper.selectById(systemInfo.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int del(List<String> ids) throws Exception {
        int updateRows = 0;
        updateRows = systeminfoMapper.deleteBatchIds(ids);
        QueryWrapper<Systemconfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("system_info_id",ids);
        updateRows = systemconfigMapper.delete(queryWrapper);
        return updateRows;
    }

    @Override
    public List<Systeminfo> load (Systeminfo systeminfo) throws Exception {
        // mybatis查询方式
        QueryWrapper<Systeminfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

      // 设置查询条件
        if (!StringUtil.isNullOrSpace(systeminfo.getSystemName())) {
            queryWrapper.like("system_Name",systeminfo.getSystemName());
        }
        if (!StringUtil.isNullOrSpace(systeminfo.getSystemType())) {
            queryWrapper.eq("system_type",systeminfo.getSystemType());
        }

        // 执行查询
        List<Systeminfo> systemconfigs = systeminfoMapper.selectList(queryWrapper);


        return systemconfigs;
    }
    @Override
    public MyPageResult<Systeminfo> loadByPaging (Systeminfo systeminfo) throws Exception {
        // begin 分页开始
        // 第几页
        int pageNum = systeminfo.getPageNum();
        // 每页的数量
        int pageSize = systeminfo.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Systeminfo> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize);
        // mybatis查询方式
        QueryWrapper<Systeminfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        // 设置查询条件
        if (!StringUtil.isNullOrSpace(systeminfo.getSystemName())) {
            queryWrapper.like("system_Name",systeminfo.getSystemName());
        }
        // pagehelper分页插件的问题，需要创建PageInfo才能获取总记录数，queryPage.getResult()和dbDataDictList是一样的
        IPage<Systeminfo> processInfoIPage = systeminfoMapper.selectPage(page, queryWrapper);
        // 执行查询
//        List<T> dbEntityList = (List<T>) dataDictMapper.selectByExample(queryExample);
        MyPageResult<Systeminfo> myPageResult = MyPageResultUtil.createMyPageResultPlus(processInfoIPage);
        return myPageResult;
        }
}